package com.ruoyi.finance.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.finance.domain.KltInvoicing;
import com.ruoyi.finance.domain.KltInvoicingDetail;
import com.ruoyi.finance.domain.VO.KltInvoicingDTO;
import com.ruoyi.finance.domain.VO.KltInvoicingVO;
import com.ruoyi.finance.domain.VO.StockVO;
import com.ruoyi.finance.mapper.KltInvoicingDao;
import com.ruoyi.finance.mapper.KltInvoicingDetailDao;
import com.ruoyi.finance.service.KltInvoicingDetailService;
import com.ruoyi.finance.service.KltInvoicingService;
import com.ruoyi.finance.service.KltStockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * (KltInvoicing)表服务实现类
 *
 * @author makejava
 * @since 2025-02-13 15:09:03
 */
@Service("kltInvoicingService")
@Transactional
public class KltInvoicingServiceImpl extends ServiceImpl<KltInvoicingDao, KltInvoicing> implements KltInvoicingService {
    @Autowired
    public KltInvoicingDao kltInvoicingDao;
    @Autowired
    public KltInvoicingDetailDao kltInvoicingDetailDao;
    @Autowired
    public KltInvoicingDetailService kltInvoicingDetailService;

    @Autowired
    public KltStockService kltStockService;

    @Override
    public List<StockVO> getSalesOrderInvoicingById(Integer salesOrderId){
        List<StockVO> stockByOrderIdAndDetail = kltStockService.getStockByOrderIdAndDetail(salesOrderId);
        //根据salesOrderId 查询所有已经开票情况。左外连接明细获取stockId 以及相关信息统计出已开票数量
        List<KltInvoicingDTO> kltInvoicingDTOS = kltInvoicingDao.selectAmountByStockId(salesOrderId);
        for (KltInvoicingDTO kltInvoicingDTO : kltInvoicingDTOS) {
            stockByOrderIdAndDetail.forEach(stockVO -> {
                if(stockVO.getStockId().equals(kltInvoicingDTO.getStockId().toString())){
                    stockVO.setInvoiceAmount(kltInvoicingDTO.getAmountSum());
                }
            });
        }
        return stockByOrderIdAndDetail;
    }

    @Override
    public Boolean saveInvoicingAndDetail(KltInvoicing kltInvoicing,List<StockVO> stockVOList)  {

        long id = IdWorker.getId();
        kltInvoicing.setInvoicingId(String.valueOf(id));

        int insert = kltInvoicingDao.insert(kltInvoicing);
        if(insert<0) {
            throw new RuntimeException("新增开票失败");
        }
        List<KltInvoicingDetail> kltInvoicingDetails = new ArrayList<>();
        for (StockVO stockVO : stockVOList) {
            KltInvoicingDetail kltInvoicingDetail = new KltInvoicingDetail();
            kltInvoicingDetail.setInvoicingId(String.valueOf(kltInvoicing.getInvoicingId()));
            kltInvoicingDetail.setStockId(Double.valueOf(stockVO.getStockId()));
            kltInvoicingDetail.setAmount(stockVO.getAmount());
            kltInvoicingDetail.setRate(Long.valueOf(stockVO.getRate()==null?0:stockVO.getRate()));

            kltInvoicingDetail.setPrice(stockVO.getOrderPrice());
            kltInvoicingDetail.setInvoicingDetailAmount(stockVO.getOrderPrice().multiply(BigDecimal.valueOf(stockVO.getAmount()==null?stockVO.getRate()==null?0:stockVO.getRate():stockVO.getAmount())));
            if ((kltInvoicingDetail.getAmount()!=null && kltInvoicingDetail.getAmount()>0 )||(kltInvoicingDetail.getRate()!=null && kltInvoicingDetail.getRate()>0)) {
                kltInvoicingDetails.add(kltInvoicingDetail);
            }
        }
        boolean b = kltInvoicingDetailService.saveBatch(kltInvoicingDetails);
        if(!b) {
            throw new RuntimeException("新增开票明细失败");
        }
        return true;
    }

    @Override
    public Boolean delInvoicing(String invoicingId) {
        KltInvoicing kltInvoicing = new KltInvoicing();
        kltInvoicing.setInvoicingId(invoicingId);
        kltInvoicing.setIsDel("1");

        int i = kltInvoicingDao.updateByInvoicingId(kltInvoicing);
        if(i==0) {
            throw new RuntimeException("删除开票失败");
        }
        KltInvoicingDetail kltInvoicingDetail = new KltInvoicingDetail();
        kltInvoicingDetail.setIsDel("1");
        int invoicingId1 = kltInvoicingDetailDao.update(kltInvoicingDetail, new QueryWrapper<KltInvoicingDetail>().eq("invoicing_id", invoicingId));
        if (invoicingId1==0) {
            throw new RuntimeException("删除开票明细失败");
        }
        return true;
    }


}


